package com.aiju.widgit.dialog;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiju.base.R;
import com.aiju.base.dialog.BaseDialog;
import com.aiju.bean.Account;
import com.aiju.common.PixValue;
import com.aiju.utils.CheckUtil;
import com.aiju.utils.T;
import com.aiju.widgit.ColorView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-03-27 17:17
 * @version: V1.0
 * @Description: 账本创建
 */
public class AccountDialog extends BaseDialog {

    private LinearLayout linearLayout;
    private EditText mEditText;
    private int selectColor = 0;

    public  static List<String> colors=new ArrayList<>();
    static {
        colors.add("#ff8d35");
        colors.add("#ffb300");
        colors.add("#1fc435");
        colors.add("#44cdbf");
        colors.add("#ff5e5e");
        colors.add("#3396ff");
    }

    private View inflate;

    public AccountDialog(Context context) {
        super(context);
    }

    @Override
    protected int getDialogStyleId() {
        return R.style.dialog;
    }

    @Override
    protected View getView() {
        inflate = mLayoutInflater.inflate(R.layout.dialog_account, null);
        linearLayout = (LinearLayout) inflate.findViewById(R.id.ly_account_colors);
        mEditText = (EditText) inflate.findViewById(R.id.ev_dialog_content);
        inflate.findViewById(R.id.tv_dialog_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity activity= (Activity) mContext;
                activity.finish();
                dismiss();
            }
        });
        inflate.findViewById(R.id.tv_dialog_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = mEditText.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    T.ToastShort("请输入账本名称");
                    return;
                }
                if (!CheckUtil.checkMemberCardName(name)) {
                    T.ToastShort("账本名称仅支持10位汉字、数字和英文");
                    return;
                }
                if (onAccount != null) {
                    onAccount.onAccount(name, selectColor);
                }
                dismiss();
            }
        });
        addColors();
        return inflate;
    }

    private void addColors() {

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(PixValue.dp.valueOf(30), PixValue.dp.valueOf(30));
        params.setMargins(0, 0, PixValue.dp.valueOf(15), 0);

        for (int i = 0; i < colors.size(); i++) {
            final int color = i;
            ColorView c = new ColorView(mContext);
            c.setColor(colors.get(i));
            c.setLayoutParams(params);
            c.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.setSelected(true);
                    selectColor = color;
                    int childCount = linearLayout.getChildCount();
                    for (int i = 0; i < childCount; i++) {
                        View childAt = linearLayout.getChildAt(i);
                        if (childAt != v) {
                            childAt.setSelected(false);
                        }
                    }
                }
            });
            if (i==0){//默认选中
                c.setSelected(true);
            }
            linearLayout.addView(c);
        }
    }

    /**
     * 设置对话框LayoutParams
     *
     * @return
     */
    public AccountDialog setLayoutParams() {
        WindowManager.LayoutParams lp = getDialogWindow().getAttributes();
        lp.width = PixValue.dp.valueOf(317);
        lp.height = PixValue.dp.valueOf(250);
        getDialogWindow().setGravity(Gravity.CENTER);
        getDialogWindow().setAttributes(lp);
        return this;
    }

    private OnAccount onAccount;

    public AccountDialog setOnAccount(OnAccount onAccount) {
        this.onAccount = onAccount;
        return this;
    }

    public AccountDialog setAccount(Account account) {
        mEditText.setText(account.getName());
        mEditText.setSelection(account.getName().length());
        linearLayout.getChildAt(Integer.valueOf(account.getColor())).performClick();
        selectColor = Integer.valueOf(account.getColor());
        ((TextView) inflate.findViewById(R.id.tv_dialog_right)).setText("完成");
        ((TextView) inflate.findViewById(R.id.tv_dialog_title)).setText("编辑账本信息");
        return this;
    }

    public interface OnAccount {
        void onAccount(String name, int color);
    }
}
